<template>
  <div id="counter">
    <div>
      <span @click="sub">-</span>
      <input
        type="text"
        style="width: 1rem; height: 1rem"
        :value="count"
        @change="changeCount"
      />
      <span @click="add">+</span>
    </div>
  </div>
</template>

<script>
import bus from '@/components/bus.js'
export default {
  components: {},
  props: {
    count: {
      default: 0,
      type: Number,
    },
    id: {
      default: 0,
      type: Number,
    },
  },
  methods: {
    add() {
      this.$emit('changeCount', this.count + 1)
    },
    sub() {
      if (this.count >= 1) {
        this.$emit('changeCount', this.count - 1)
      }
    },
    changeCount(e) {
      if (Number(e.target.value) <= 0) {
        console.log(Number(e.target.value)), this.$emit('changeCount', 0)
      } else {
        this.$emit('changeCount', Number(e.target.value))
      }
    },
  },
}
</script>

<style lang="less">
#counter {
  text-align: center;
  font-size: 2rem;

  div {
    display: flex;
    text-align: center;
    line-height: 1rem;

    span {
      display: block;
    }
  }
}
</style>
